import { _decorator, Component, instantiate, Label, Node, Prefab } from 'cc';
import { LayerBase } from './LayerBase';
import { UserInfo } from './UserInfo';
import { StartSceneGameMgr } from './StartSceneGameMgr';
import { UILayerMgr } from './UILayerMgr';
import { EquipCellItem } from './EquipCellItem';
import { PetItem } from './PetItem';
import { Start } from './Start';
import { RoleCellItem } from './RoleCellItem';
import { randomNum } from '../utils/utils';
import { rewardEnum } from '../Enum';
import { AudioMgr } from '../utils/AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('SalvageRolePanel')
export class SalvageRolePanel extends LayerBase {
   
    @property(Node)
    roleContent:Node;
    @property(Prefab)
    roleItemPre:Prefab;
    @property(Label)
    getSalvageValue:Label;

    role;

    init(somedData?: any): void {
        this.role=somedData;
        this.roleContent.removeAllChildren();
        let pre=instantiate(this.roleItemPre)
        pre.setParent(this.roleContent);
        pre.getComponent(RoleCellItem).init(somedData);

        this.getSalvageValue.string=somedData.salvageGet+"";

    }

    onclickSalvageBtn() {
        AudioMgr.ins.playOneShot("audio/clickBtn",0.8);

        UserInfo.crystalMun+=this.role.salvageGet;
        let rewardCongfi=UserInfo.rewardCongfiDic.get(rewardEnum.crystal);
        rewardCongfi.count=this.role.salvageGet;
        UILayerMgr.getIns.openUILayer("getRewardPanel", rewardCongfi);

        let deleteRoleId=this.role.roleId;
        UserInfo.hasRoleDic.delete(this.role.roleId)
        let nextRoleId=UserInfo.hasRoleDic.getKeyList()[randomNum(0,UserInfo.hasRoleDic.lenght-1)]

        if (UserInfo.currGoBattleRoleId==deleteRoleId) {
            UserInfo.currGoBattleRoleId=nextRoleId;
            StartSceneGameMgr.ins.roleSelectId=UserInfo.currGoBattleRoleId;
        }
        else{
            StartSceneGameMgr.ins.roleSelectId=UserInfo.currGoBattleRoleId;
        }

        StartSceneGameMgr.ins.saveGameDate();
        UILayerMgr.getIns.resfreshUI("equipPanel")
        Start.ins.initHomePanel();
        UILayerMgr.getIns.closeUILayer(this.node.name);
    }

}


